
/**
 * 通过一些关键字typeof instanceof for in 来缩小范围
 */
export {};
function double(input: string | number) {
  if (typeof input === "string") {
    console.log(input);
  } else if (typeof input === "number") {
    console.log(input);
  }
}

class Animal {}
class Bird extends Animal {}
class Dog extends Animal {}
function getName(animal: Animal) {
  if (animal instanceof Bird) {
    console.log(animal);
  } else if (animal instanceof Dog) {
    console.log(animal);
  }
}
//null 保护
function getFirstLetter(s: string | null) {
  // if(s===null){ //方法1
  //     return ''
  // }
  s = s || ""; //方法2
  return s!.charAt(0); //方法3 ！
}

//链判断运算符 ts非常新也不支持
let a = {};
//a?.b()
//a?.[x]

//可辨识的联合类型
interface WarningButton {
  class: "warning";
  text1: "修改";
}
interface DangerButton {
  class: "danger";
  text2: "删除";
}
type Button = WarningButton | DangerButton;
function getButton(button: Button) {
  if (button.class === "warning") {
    console.log(button);
  }
  if (button.class === "danger") {
    console.log(button);
  }
}

interface User {
  username: string;
}
type Action =
  | {
      type: "add";
      payload: User;
    }
  | { type: "delete"; payload: number };

  const reducer = (ation:Action)=>{
    switch(actions.type){
        case 'add':
            actions.payload.username
            break
        case 'delete':
            let id:number = ation.payload;
            break
    }
  }

  
